//
//  UIBarButtonItem+category.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/3/14.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit
extension UIBarButtonItem{
    ///创建一个自定义Item的方法
    class func creatBarButtonItem(imageName: String, target: Any?, action: Selector) -> UIBarButtonItem {
        //自定义一个导航栏
        let btu = UIButton()
        btu.setBackgroundImage(UIImage.init(named: imageName), for: UIControlState.normal)
        btu.setBackgroundImage(UIImage.init(named: imageName + "_highlighted"), for: UIControlState.highlighted)
        btu.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        btu.sizeToFit()
        return UIBarButtonItem.init(customView: btu)
    }
    
    //convenience便利构造器 一办在extension里面使用 是横向调用构造 只调用本类的构造犯法 不能super 只能self.init()
    
    /// 使用init防来快速的创造一个按钮
    ///
    /// - Parameters:
    ///   - imageName: <#imageName description#>
    ///   - target: <#target description#>
    ///   - action: <#action description#>
    convenience init(imageName: String, tag:Int, target: Any?, action: Selector) {
        let btu = UIButton()
        btu.setBackgroundImage(UIImage.init(named: imageName), for: UIControlState.normal)
        btu.setBackgroundImage(UIImage.init(named: imageName + "_highlighted"), for: UIControlState.highlighted)
        btu.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        btu.tag = tag
        btu.sizeToFit()
        
        self.init(customView: btu)
    }
}
